<template>
  <div>
    <p @click="ad">点击</p>
  </div>
</template>

<script>
export default {
  data () {
    return {
      ws:'',
    }
  },
  created () {
    // 初始化
    this.init()
  },


  methods: {
    ad(){
      this.axios.get("http://localhost:9090/system/users/loginWebSocket")
    },
    init: function () {
      alert("实例化websocket")
      if (typeof (WebSocket) === "undefined") {
        alert("您的浏览器不支持socket")
      } else {
        // 实例化socket
        //userId是你要发送的用户id
        this.socket = new WebSocket("ws://localhost:9090/webSocketToUser/" + 72)
        // 监听socket连接
        this.socket.onopen = this.open
        // 监听socket错误信息
        this.socket.onerror = this.error
        // 监听socket消息
        this.socket.onmessage = this.getMessage
      }
    },
    open: function () {
      console.log("socket连接成功")
    },
    error: function () {
      console.log("连接错误")
    },
    getMessage: function (msg) {
      this.openMsg(msg.data)
    },
    send: function () {
      this.socket.send(params)
    },
    close: function () {
      console.log("socket已经关闭")
    },
    openMsg(msg) {
      let _self = this;
      this.$notify({
        title: '提示',
        message: msg,
        duration: 0,
        type: 'warning',
        onClose: function () {
        }
      })
    }
  }
}

</script>

<style>

</style>
